-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Fix multiple clippy issues #143423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix multiple clippy issues #143423
Conversation
Some changes occurred to the intrinsics. Make sure the CTFE / Miri interpreter cc @rust-lang/miri, @RalfJung, @oli-obk, @lcnr Some changes occurred to the platform-builtins intrinsics. Make sure the cc @antoyo, @GuillaumeGomez, @bjorn3, @calebzulawski, @programmerjake |
679953e
to
1e803f0
Compare
do we run a subset of clippy lints in CI? Is there some overarching issue tracking this work? |
regarding overarching issue, see #143367 (comment) |
@oli-obk I don't know if any clippy lints run in CI, but I do know that many don't, given the amount of warnings generated by clippy atm. Given the volume of warnings it is very hard to know if there is anything useful in there. Clippy can be a very useful tool in my experience, so I'd like to slowly cut down on the noise. If that succeeds then it seems a good idea to enable clippy in CI to keep the warnings down. @jhpratt has suggested that I should obtain some support/approval for this. I've started my work on core, with std and the compiler possibly after. So @rust-lang/libs, @rust-lang/compiler , can I ask that you consider this? |
Clippy is run in CI https://github.com/rust-lang/rust/blob/master/src/bootstrap/src/core/build_steps/clippy.rs. Not member, but if some documentation clean is probably ok, changes like 819d4a3 not immediately obviously (for me) better, they (mostly) miss codegen tests and fixing clippy lint can instead make code slower. |
This is true, so there is always the alternative of allowing (or expecting) the lint to silence clippy. |
I asked about this on zulip a while back, |
@yotamofek thanks for the link. Personally I found the discussion there quite encouraging and I feel supported in working on this. It was very useful to learn about where clippy is already applied in CI, which gives the precise list of lints that are currently enabled. I am not at all proposing to enable all default lints. Right now, all I really need is that it is deemed acceptable to allow lints locally. This would also prevent future bad PRs from people mindlessly following clippy, which was one of the things that were brought up as clippy negatives. This could make it possible to at some point change the global list. Another point brought up was that the default list changes (too much) with each clippy update to enable all lints from the default list. My hope is that more interaction between clippy and rustc will make both better. One example of clippy getting better is: rust-lang/rust-clippy#15200. That change alone prevents hundreds of false positives in rustc, once the next clippy update is merged. And maybe we can get to a point where changes to the default lint list will be checked against rustc before being made. rustc could be a lithmus test for clippy, improving its quality, while at the same time we can try to take more and more advantage of clippy where it works for rustc. Anyway, my current plan is to work through what clippy reports for core. Make changes where appropriate, but also allow lints where they seem bad or don't apply, and report false positives upstream. Along the way I hope to get a better sense of what's possible. |
@hkBst Could you please update your PR initial comment with a recap of the lints you added, possibly with a little bit of context about the reasoning? That could help casual reviewers of this patch get a quick summary. Thanks 🙂 |
@apiraino I tried to separate out the different parts into separate commits, which name the thing that clippy complains about, but I will mention that in the summary. I think that would help with most of them, except for one which I will try and explain a bit more. I ended up listing the commits with a small explanation for each. |
Personally (and my opinion really shouldn't matter that much to anyone 😝) I don't like seeing Other than that, yeah, I'm happy to see lints being fixed in the codebase. |
1e803f0
to
76d9775
Compare
@yotamofek the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a library reviewer, but speaking as a reviewer generally. I feel like instead of shotgun-applying a bunch of clippy lints, I would strongly encourage the approach of #general > Running clippy on the rustc repo @ 💬:
fwiw when i made
x clippy
work i very intentionally did not try to fix any of the lints, because i think it would be a shame if we blindly applied them all.i suspect you are right that there are maybe 5-15 lints we could turn on that would be useful and worth the time to fix. but i would be strongly against just doing
RUSTFLAGS=-Dwarnings x clippy
because most of them are just not that importantfiguring out what those 5-15 lints are would be a good use of time though :) "i think this lint should be on because X" is much more useful than "cargo fix changed the code and i made a pr with the diff"
And if these subset of clippy lints reviewers find generally applicable and materially improves readability (or some other metric), we should include them into the ./x clippy ci
subset so that they're actually CI-checked.
I don't subscribe to the argument of
the
allow(..)
annotations do add a tiny bit of noise, but what we get in return is fewer PRs from people trying to fix what is not broken, and potentially making it possible to enable these lints later.
I'll take changes which I find materially better, and having a lot of #[allow(clippy::*)]
unchecked in CI I don't find is materially better, they just seem noisy...
Even
deemed acceptable to allow lints locally
I don't think justifies having a lot of #[allow(clippy::*)]
in the codebase, especially if the allowed clippy lints are non-considerations.
library/core/src/cmp.rs
Outdated
fn default_chaining_impl<T: PartialOrd<U> + PointeeSized, U: PointeeSized>( | ||
lhs: &T, | ||
rhs: &U, | ||
p: impl FnOnce(Ordering) -> bool, | ||
) -> ControlFlow<bool> | ||
where | ||
T: PartialOrd<U>, | ||
{ | ||
) -> ControlFlow<bool> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remark: I don't find this materially better
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is slightly better, but perhaps not rising to materially.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move everything into the where
clauses instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the lint is pretty valid in that it's nicer to read all bounds in either the where
or the generics, not split. 2cents: I think when there are multiple bounds on multiple generics, it usually looks nicer to do the opposite fix and move them all to where
.
(whoops, race with Jubilee)
library/core/src/mem/mod.rs
Outdated
@@ -803,6 +803,7 @@ pub const fn swap<T>(x: &mut T, y: &mut T) { | |||
#[inline] | |||
#[stable(feature = "mem_take", since = "1.40.0")] | |||
pub fn take<T: Default>(dest: &mut T) -> T { | |||
#[allow(clippy::mem_replace_with_default)] // exempt by being the one true definition |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remark: or having a lot of #[allow(clippy::*)]
that we don't run in CI
library/core/src/num/fmt.rs
Outdated
} else if v < 10_000 { | ||
4 | ||
} else { | ||
if v < 10_000 { 4 } else { 5 } | ||
5 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remark: and debatable, but IMO this isn't materially better either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO the vertical spacing is unfortunate, but eliminating extra braces seems worthwhile, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is open-coding a log10 operation, cleaned this up here: #143540
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yotamofek good move! I can't wait to see perf results.
if you are looking for approval to insert allow directives for clippy, consider opening a compiler MCP: https://forge.rust-lang.org/triagebot/major-changes.html |
.. wait these are library changes. so a library MCP or something. sorry im on mobile |
When doing library work, clippy admonishing me about implementing something by-hand that is available in std is useless noise. I do not wish for these lints to be enabled for std, as we often must by-hand reimplement parts of std in order to guarantee good performance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
library/core/src/num/f32.rs
Outdated
pub const NAN: f32 = 0.0_f32 / 0.0_f32; | ||
/// Infinity (∞). | ||
#[stable(feature = "assoc_int_consts", since = "1.43.0")] | ||
#[allow(clippy::zero_divided_by_zero)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dividend isn't even zero. Why is clippy's "zero_divided_by_zero" lint firing on a division of not-zero by zero?
library/core/src/num/fmt.rs
Outdated
@@ -19,6 +19,7 @@ pub enum Part<'a> { | |||
|
|||
impl<'a> Part<'a> { | |||
/// Returns the exact byte length of given part. | |||
#[allow(clippy::len_without_is_empty)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I do not want this lint on internals of std.
library/core/src/num/flt2dec/mod.rs
Outdated
None | ||
} | ||
None if d.len() > 0 => { | ||
None if !d.is_empty() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not find !x.is_empty()
easier to read than d.len() > 0
, frankly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I sympathize with that statement, but is_empty
is in std, presumably because it makes it easier to express intent and thus be clearer and thus also easier to read. If the not
is a problem, then this branch can always be switched with the next, I suppose.
@@ -55,7 +55,7 @@ pub fn compute_float<F: RawFloat>(q: i64, mut w: u64) -> BiasedFp { | |||
// <https://arxiv.org/pdf/2101.11408.pdf#section.9.1>. For detailed | |||
// explanations of rounding for positive exponents, see | |||
// <https://arxiv.org/pdf/2101.11408.pdf#section.8>. | |||
let inside_safe_exponent = (q >= -27) && (q <= 55); | |||
let inside_safe_exponent = (-27..=55).contains(&q); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is extremely unlikely this is equivalent codegen in such a sensitive case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have #45222 as the classic example of inclusive ranges being optimized worse. I would hope that since the range bounds are constant this use gets optimized well. But I appreciate the caution because the range types are quite complicated now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was curious so I checked, and it seems both variations optimize down to the same 3 instructions: https://godbolt.org/z/6WP87jPK9
(that, though, doesn't necessarily mean it will codegen the same in this context, inside a larger function, obviously...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the specifics, this can easily be switched to (-27..56).contains(&q)
. I was just trying to be conservative.
library/core/src/cmp.rs
Outdated
fn default_chaining_impl<T: PartialOrd<U> + PointeeSized, U: PointeeSized>( | ||
lhs: &T, | ||
rhs: &U, | ||
p: impl FnOnce(Ordering) -> bool, | ||
) -> ControlFlow<bool> | ||
where | ||
T: PartialOrd<U>, | ||
{ | ||
) -> ControlFlow<bool> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move everything into the where
clauses instead.
While Jubilee took over assignment, I would still prefer that T-libs weigh in before there are numerous PRs like this merged. That was the reason I had explicitly not reviewed any of ones already open. Per asking on Zulip, nominating for the general case of whether we want clippy on the standard library. If so, which ones and/or how should reviewers determine what is appropriate and what PRs to close? |
Yes, that's why I singled out the commits that are formatting-only changes. I do not think a few indentation fixups are worth larger discussion. |
76d9775
to
ce51089
Compare
ce51089
to
dc23317
Compare
I've abandoned adding clippy annotations, since these don't seem to have any support. Moving forwards, I will see what can be done about clippy lints, without introducing such annotations. flt2dec/mod.rs is probably the most (and only?) contentious change of this PR. |
.len() == 0
.